﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEditor;
using UnityEngine;
using System.Data.Odbc;
using System.Data;
using System.IO;

public class EditorHelp
{
    public static void ReadXlsFile(string fileName, string[] sheetNames, out DataSet ds)
    {
        ds = new DataSet();
        if (File.Exists(fileName))
        {
            //bool hasDriver = true;
            string con = string.Concat("Driver={Microsoft Excel Driver (*.xls)}; DriverId=790; Dbq=", fileName, ";HDR=Yes;IMEX=1;");
            using (OdbcConnection oCon = new OdbcConnection(con))
            {
                using (OdbcCommand oCmd = oCon.CreateCommand())
                {
                    using (OdbcDataAdapter oAp = new OdbcDataAdapter(oCmd))
                    {
                        try
                        {
                            oCon.Open();
                            for (int i = 0; i < sheetNames.Length; i++)
                            {
                                oCmd.CommandText = string.Format("SELECT * FROM [{0}$A:BZ]", sheetNames[i]);
                                oAp.Fill(ds, sheetNames[i]);
                            }
                        }
                        catch (Exception e)
                        {
                            EditorUtility.DisplayDialog("Error", e.ToString(), "OK");
                            //hasDriver = false;
                        }
                        finally
                        {
                            oCon.Close();
                        }
                    }
                }
            }
        }
        else
        {
            throw new Exception(string.Concat("File \"", fileName, "\" is not found or you need to change config name."));
        }
    }
}

